from manim import *



class math4(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{(2018)设}$f\left( x\right) $\text{在}$\left[ 0,1\right] $\text{二阶可导,且}$\int _{0}^{1}f\left( x\right) dx=0$\text{则}$()$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,0,0])
        
        text2 = Tex(r"$\left( A\right) $\text{当}$f'\left( x\right) <0$\text{时,}$f\left( \frac{1}{2}\right)  <0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-5,0])
        text3 = Tex(r"$\left( B\right) $\text{当}$f''\left( x\right) <0  $\text{时,}$f\left( \frac{1}{2}\right)  <0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-5,0])
        text4 = Tex(r"$\left( C\right) $\text{当}$f'\left( x\right) >0 $\text{时,}$f\left( \frac{1}{2}\right)  >0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-10,0])
        text5 = Tex(r"$\left( D\right) $\text{当}$f''\left( x\right) >0$\text{时,}$f\left( \frac{1}{2}\right)  <0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-10,0])
        self.play(Write(text),Write(text2),Write(text3),Write(text4),Write(text5),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([-10,20,0]) 
                  ,text2.animate().move_to([-25,15,0]) ,text3.animate().move_to([25,15,0])
                   ,text4.animate().move_to([-25,10,0]) ,text5.animate().move_to([25,10,0]) )

        rectangle=Rectangle(color=BLUE,fill_opacity=0.3,width=10,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-10.5, 20, 0])
        
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.3,width=12,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([21, 15, 0])
        
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.3,width=12,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.move_to([21, 10, 0])
        self.play(Create(rectangle),Create(rectangle1),Create(rectangle2))
        text6 = Tex(r"\text{泰勒不等式}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle2,DOWN,buff=1)
        self.play(Write(text6))

        text7 = Tex(r"$f\left( x\right) =f\left( x_{0}\right) +f'\left( x_{0}\right) \left( x-x_{0}\right) +\dfrac{f''\left( \xi \right) }{2}\left( x-x_{0}\right) ^{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,0,0])
        text8 = Tex(r"\text{(抽象泰勒等式)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text7,RIGHT,buff=3)
        self.play(Write(text7),Write(text8))
        
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.3,width=6,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle3.move_to([34, 15, 0])
        
        rectangle4=Rectangle(color=BLUE,fill_opacity=0.3,width=6,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle4.move_to([34, 10, 0])
        self.play(Create(rectangle3),Create(rectangle4))
        text9 = Tex(r"\text{取}$x_{0}=\dfrac{1}{2}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text8,RIGHT,buff=3)
        self.play(ReplacementTransform(rectangle4,text9),FadeOut(rectangle3))
        self.wait(0.2)
        text10 = Tex(r"$f\left( x\right) =f\left( \frac{1}{2}\right) +f'\left( \frac{1}{2}\right) \left( x-\frac{1}{2}\right) +\frac{f''\left( \xi \right) }{2}\left( x-\frac{1}{2}\right) ^{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,0,0])
        text88 = Tex(r"\text{(泰勒等式)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text7,RIGHT,buff=3)
        self.play(ReplacementTransform(text9,text10),ReplacementTransform(text7,text10),ReplacementTransform(text8,text88))
        self.wait(0.2)
        text11 = Tex(r"$f\left( x\right) \leq  f\left( \frac{1}{2}\right) +f'\left( \frac{1}{2}\right) \left( x-\frac{1}{2}\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-5,0])
        text110 = Tex(r"$f\left( x\right) \geq f\left( \frac{1}{2}\right) +f'\left( \frac{1}{2}\right) \left( x-\frac{1}{2}\right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-10,0])
        text12 = Tex(r"\text{(泰勒不等式)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text110,DOWN,buff=3)
        
        self.play(ReplacementTransform(rectangle1,text11),ReplacementTransform(rectangle2,text110),Write(text12),FadeOut(text6))
        self.wait(0.2)
        arrow = Arrow(start=[-12,-7.5,0],end=[-7,-7.5,0],color=RED
                       , stroke_width=20, tip_length=1)
        self.play(GrowArrow(arrow))

        text13 = Tex(r"$\int _{0}^{1}f\left( x\right) dx \leq \int _{0}^{1}f\left( \frac{1}{2}\right) dx+\int _{0}^{1}f'\left( \frac{1}{2}\right) \left( x-\frac{1}{2}\right) dx $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-5,0])
        text14 = Tex(r"$\int _{0}^{1}f\left( x\right) dx \geq \int _{0}^{1}f\left( \frac{1}{2}\right) dx+\int _{0}^{1}f'\left( \frac{1}{2}\right) \left( x-\frac{1}{2}\right) dx $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-10,0])
        text15 = Tex(r"\text{(积分不等式)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,DOWN,buff=3)
        self.play(Write(text13),Write(text14),Write(text15))
        rectangle6=Rectangle(color=BLUE,fill_opacity=0.3,width=3,height=8
                            ,stroke_color=BLUE,stroke_width=15)
        rectangle6.move_to([24, 12.5, 0])
        self.play(Create(rectangle6))
        rectangle5=Rectangle(color=BLUE,fill_opacity=0.3,width=3,height=9
                            ,stroke_color=BLUE,stroke_width=15)
        rectangle5.move_to([7.5, -8, 0])
        self.play(Create(rectangle5))

       

        text130 = Tex(r"$\int _{0}^{1}f\left( x\right) dx < \int _{0}^{1}f\left( \frac{1}{2}\right) dx+\int _{0}^{1}f'\left( \frac{1}{2}\right) \left( x-\frac{1}{2}\right) dx $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-5,0])
        text140 = Tex(r"$\int _{0}^{1}f\left( x\right) dx > \int _{0}^{1}f\left( \frac{1}{2}\right) dx+\int _{0}^{1}f'\left( \frac{1}{2}\right) \left( x-\frac{1}{2}\right) dx $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-10,0])
        
        self.play(ReplacementTransform(text13,text130),ReplacementTransform(text14,text140),FadeOut(rectangle6,rectangle5))
        self.wait(0.2)
        rectangle6=Rectangle(color=GREEN,fill_opacity=0.3,width=12.5,height=9
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle6.move_to([0, -8, 0])
        text16 = Tex(r"\text{0}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle6,DOWN,buff=2)
        self.play(Create(rectangle6),Write(text16), rectangle.animate.move_to([7.5,20,0]).stretch_to_fit_width(17),FadeOut(text15))

        rectangle7=Rectangle(color=GREEN,fill_opacity=0.3,width=12.5,height=9
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle7.move_to([15, -7.5, 0])
        text17 = Tex(r"$f\left( \frac{1}{2}\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle7,DOWN,buff=2)
        self.play(Create(rectangle7),Write(text17))

        rectangle8=Rectangle(color=GREEN,fill_opacity=0.3,width=22.5,height=9
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle8.move_to([35.5, -7.5, 0])
        text18 = Tex(r"$0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle8,DOWN,buff=2)
        self.play(Create(rectangle8),Write(text18))

        #半透明矩阵扩展
        rectangle9=Rectangle(color=RED,fill_opacity=0.5,width=1,height=3
                            ,stroke_color=YELLOW,stroke_width=0).move_to([25,15,0])
        rectangle10=Rectangle(color=YELLOW,fill_opacity=0.5,width=1,height=3
                            ,stroke_color=YELLOW,stroke_width=0).move_to([25,10,0])
        self.play(
            rectangle9.animate.stretch(33.0,dim=0),
            rectangle10.animate.stretch(33.0,dim=0)
        )


        
        self.wait()